package com.white.commonlib.engine.TextImgMixer;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.widget.TextView;

import com.white.commonlib.engine.img.DefaultImageLoader;
import com.white.commonlib.engine.img.IImageLoader;
import com.white.commonlib.manager.LogManager;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Created by Administrator on 2017/5/4 0004.
 */

public class ImgGetter implements Html.ImageGetter
{
    TextView tv;
    Context mContext;
    IImageLoader loader;
    int containerWidth;
    Map<String, UrlDrawable> bmps;

    public ImgGetter(TextView tv, Context ctx)
    {
        this.tv = tv;
        this.mContext = ctx;
        loader = new DefaultImageLoader(ctx);
        bmps = new HashMap<>();
    }

    public void setContainerWidth(int wid)
    {
        containerWidth = wid;
//        LogManager.e("设置宽度 = " + containerWidth);
        isContainerWidthSetted = true;
    }

    boolean isContainerWidthSetted;
    public boolean isContainerWidthSetted()
    {
        return isContainerWidthSetted;
    }

    @Override
    public Drawable getDrawable(final String source)
    {
        UrlDrawable wkCache = bmps.get(source);
        if (wkCache != null)
        {
            return wkCache;
        } else
        {
            final UrlDrawable ud = new UrlDrawable(mContext);
            LogManager.e("图片地址：" + source);
            loader.decodeImage(source, true, new IImageLoader.OnDecodeResultCallback()
            {
                @Override
                public void onComlete(Bitmap bmp)
                {
                    if(containerWidth == 0)
                        return;
                    int w = bmp.getWidth();
                    int h = bmp.getHeight();

                    //如果图片较小则默认放大1.8倍
                    float scale = 1.8f;
                    Matrix m = new Matrix();

                    LogManager.e("w=" + w + " ---- h=" + h + " ---- containerWidth=" + containerWidth);

                    if(w > containerWidth /2)
                    {
                        scale = containerWidth * 1.0f / w;
                        m.postScale(scale, scale);
                    }else
                    {
                        m.postScale(scale, scale);
                    }
                    if(w > 0 && h > 0)
                    {
                        bmp = Bitmap.createBitmap(bmp, 0, 0, w, h, m, false);
                        ud.setBmp(bmp);
                        ud.setBounds(0, 0, bmp.getWidth(), bmp.getHeight());
                        bmps.put(source, ud);

                        tv.postInvalidate();
                        tv.post(new Runnable()
                        {
                            @Override
                            public void run()
                            {
                                tv.setText(tv.getText());
                            }
                        });
                    }
                }

                @Override
                public void onFailed(String s)
                {
                    LogManager.e("图片加载失败：" + s);
                }
            });
            return ud;
        }
    }

    public void recycle()
    {
        if (bmps != null && bmps.size() > 0)
        {
            Set<String> set = bmps.keySet();
            for (Iterator<String> it = set.iterator(); it.hasNext(); )
            {
                UrlDrawable wd = bmps.get(it.next());
                if (wd != null)
                {

                    wd.recycle();
                }
            }
            bmps.clear();
        }
    }
}
